//
//  ViewController.m
//  NSOpration 多线程
//
//  Created by 王苏进 on 3/5/17.
//  Copyright © 2017年 王苏进. All rights reserved.
//

#import "ViewController.h"
#import "WSJOperation.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    [self test1];
}


- (void)tsst {
 
//    抽象类
//    NSOperation
    
    
//    子类
//    NSInvocationOperation
//     NSBlockOperation
    
//    
//     NSOperationQueue

}

- (void)test1 {

  
    NSInvocationOperation *opration = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test1Action) object:nil];
    [opration start];
    
}
- (void)test1Action{
    NSLog(@"%s---- %@",__func__,[NSThread currentThread]);
    
}



- (void)invocationOpration{
   
 
    NSBlockOperation *op1 =[NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"1---- %@",[NSThread currentThread]);
    }];
    [op1 start];
    
    //追加任务
//    op1 addExecutionBlock:<#^(void)block#>
    
}



- (void)oprationQuere {

    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    
    NSBlockOperation *op1 =[NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"1---- %@",[NSThread currentThread]);
    }];
    NSBlockOperation *op2 =[NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"2---- %@",[NSThread currentThread]);
    }];
    NSBlockOperation *op3 =[NSBlockOperation blockOperationWithBlock:^{
        
        NSLog(@"3---- %@",[NSThread currentThread]);
    }];

    //依赖要在  加入队列之前操作 2  依赖 1 ， 1 依赖3  所以执行顺序 事 3- 1- 2
    [op2 addDependency:op1];
    [op1 addDependency:op3];
    
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];
   
    
   //不需要手动启动start ，添加到队列中 会自动开启任务
//    [op1 start];
//    [op2 start];

    
}

- (void)customOperation{
   
    
//    优点：有利于隐蔽代码
//         提高代码的复用性
    
    WSJOperation *op1 = [WSJOperation new];
    WSJOperation *op2 = [WSJOperation new];
    
    
    NSOperationQueue *queue = [NSOperationQueue new];
    
    [queue addOperation:op1];
    [queue addOperation:op2];
    
    

}


- (void)maxOperationCount{
  
    NSOperationQueue *queue = [NSOperationQueue new];
    
    queue.maxConcurrentOperationCount = 1;
    
    //同一时间 最多有多少个任务可以执行
    
//    串行执行任务 ！= 只开一条线程
    
    //maxConcurrentOperationCount > 1 就是并发队列 ， = 1 串行队列， = 0 不会至执行任何 任务
    // 默认值 = -1 ，  标识最大并发数 不受限制
    
    NSBlockOperation *op1 =[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"1111 --- %@",[NSThread currentThread]);
    }];
    NSBlockOperation *op2 =[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"222 --- %@",[NSThread currentThread]);
    }];
    NSBlockOperation *op3 =[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"3333 --- %@",[NSThread currentThread]);
    }];
    NSBlockOperation *op4 =[NSBlockOperation blockOperationWithBlock:^{
       NSLog(@"4444 --- %@",[NSThread currentThread]);
    }];
    NSBlockOperation *op5 =[NSBlockOperation blockOperationWithBlock:^{
     NSLog(@"5555 --- %@",[NSThread currentThread]);
        
    
    }];
    NSBlockOperation *op6 =[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"6666 --- %@",[NSThread currentThread]);
    
    }];
  
     [queue addOperation:op1];
     [queue addOperation:op2];
     [queue addOperation:op3];
     [queue addOperation:op4];
     [queue addOperation:op5];
     [queue addOperation:op6];
    
   
    
}

- (void)pauseAndCancel{
  
    
    
    
}




- (void)downloadImage{
  
    //线程间通信
    
// http://imgsrc.baidu.com/forum/pic/item/818dca1b0ef41bd54d393f0351da81cb38db3d77.jpg
    
    NSOperationQueue *queue = [NSOperationQueue new];
    
    
    NSBlockOperation *download = [NSBlockOperation blockOperationWithBlock:^{
        NSURL *url = [NSURL URLWithString:@"http://imgsrc.baidu.com/forum/pic/item/818dca1b0ef41bd54d393f0351da81cb38db3d77.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.imageView.image = image;
        }];
    }];
    
    
    [queue addOperation:download];
    
    

}

- (void)GCD_ThreadNoti{
   
//     block 里面嵌套就可以了

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self downloadImage];
}




@end
